﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
namespace XiuXiuTools
{
    public partial class EmpListForm : XiuXiuTools.BaseForm
    {
        public EmpListForm()
        {
            InitializeComponent();
        }
        DataTable dtUserType = new DataTable();
        DataTable dtUserCMB = new DataTable();
        LightXiuCore.LightData.XIU_EMPDataTable UserList = new LightXiuCore.LightData.XIU_EMPDataTable();
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            dtUserType = serviceClient.GetDataBySql("SELECT * FROM zjwdb_165769.xiu_emp_type;", "userType");
            dtUserCMB = serviceClient.GetDataBySql("SELECT * FROM zjwdb_165769.xiu_emp_type;", "userType");
            var dt = serviceClient.GetDataBySql("SELECT * FROM zjwdb_165769.xiu_emp;", "userType");
            foreach (DataRow row in dt.Rows)
            {
                UserList.Rows.Add(row.ItemArray);
            }
            UserList.AcceptChanges();

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            cmbUserType.DataSource = dtUserType;
            cmbUserType.DisplayMember = "EMP_TYPE_NAME";
            cmbUserType.ValueMember = "EMP_TYPE_ID";
            colType.DataSource = dtUserCMB;
            colType.DisplayMember = "EMP_TYPE_NAME";
            colType.ValueMember = "EMP_TYPE_NAME";
            dataGridViewData.DataSource = UserList;
            SetControlsEnable(this, true);
        }

        private void EmpListForm_Load(object sender, EventArgs e)
        {
            SetControlsEnable(this, false);
            dataGridViewData.AutoGenerateColumns = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var rowTxt = UserList.Where(wh =>wh.RowState!=DataRowState.Deleted&&wh.RowState!=DataRowState.Detached &&  wh.EMP_NAME == txtName.Text);
            if (rowTxt.Count()>0)
            {
                return;
            }
            UserList.AddXIU_EMPRow(Guid.NewGuid().ToString(), cmbUserType.Text, txtName.Text, 1);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          
            backgroundWorker2.RunWorkerAsync();
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            serviceClient.UpdateData(UserList, "SELECT * FROM zjwdb_165769.xiu_emp where 1=2 ;", "");

        }

        private void cmbUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridViewData.DataSource = UserList.Where(wh => wh.EMP_TYPE.Trim() == "" || wh.EMP_TYPE == cmbUserType.Text).AsDataView() ;
        }
    }
}
